<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
  <head>
    <title>Visualize homology</title>
    <meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
    <link rel="stylesheet" href="visblaststylejw.css" type="text/css">
    <?php include('jasonweirather.php'); ?>
  </head>
  <body>
    <?php
      $rname = rand();
      $tname = date('Y-m-d');
      $basename="visblastOut_" . "$rname" . "_" . "$tname";
      $fasta = $_POST["fasta"];  
      $fastainf = NULL;
      if($_FILES['fastainf']['size']<500000000  && $_FILES['fastainf']['size'] > 0) { 
        $fastainf = file_get_contents($_FILES['fastainf']['tmp_name']); 
      }
      if($fasta == NULL) { $fasta = $fastainf; }
      $in_autocolor = $_POST["autocolor"];
      $autocolor = '';
      if($in_autocolor == 'doauto') { $autocolor = '--autocolor'; }
      $ivalue = $_POST["ivalue"];
      $fasta = preg_replace('/\r/',"",$fasta);
      $fastaCheck = NULL;
      $no_error = 0;
      if($fasta == NULL) { $no_error = 1; } # first time dont show an error
      if($fasta != NULL) { $fastaCheck = checkFasta($fasta); }
      if($fastaCheck != 'good'  && $fasta != NULL) { $fasta = NULL; }
      #echo "$fasta<br>\n";
      $ref_string='Jason Weirather, Mary Wilson, John Donelson.  Manuscript in preparation 2010.';
      $title=htmlentities($_POST["title"]);
      $evalue=$_POST["evalue"];
      $evalue=checkEvalue($evalue);
      $ivalue=checkEvalue($ivalue);
      if($ivalue < (float)'1.04' || $ivalue > (float)'8.0') { $ivalue = NULL; }
      if($ivalue == NULL) { $ivalue = 2.0; }
      $haveinputs = 0;
      if($fasta != NULL && $title != NULL && $evalue != NULL) {
        $haveinputs = 1;
      }
      if($haveinputs == 0) {
        echo "<div id=\"main_page_one\">\n";
        
      }
      if($haveinputs == 1) {
        echo "<div id=\"main_page_two\">\n";
      }
      echo "      <div id=\"top_title\">Visualize clusters of genes based on homology</div>\n";
      if($haveinputs == 1) {
        #echo "hi!<br>hi!<br>";
        #echo "Base name:  $basename<br>\n";
        #pass the ball to visblast
        ### you really need to check the inputs that will go in to this
        ### right now it should just be evalue.  don't get killed out there.
        ###  change this path when you change computers!!!
        $visBlast2path = "/home/x/Code/VISBLASTWEB";
        $workingdirectory = "/home/x/Public/visblast/work";
        $working_address = "http://localhost/visblast/work";
        #make the fasta based off the base name
        $fastafilename = "$workingdirectory/$basename.fasta";
        $fh = fopen($fastafilename,'w') or die ("can't open file");
        fwrite($fh,$fasta);
        fclose($fh);
        #$evalue = whatever
        $titlefilename = "$workingdirectory/$basename.title";
        $fh = fopen($titlefilename,'w') or die ("can't open file");
        fwrite($fh,$title);
        fclose($fh);
        $command = "$visBlast2path" . "/visBlast2.pl -i $workingdirectory/$basename.fasta -e $evalue --svg $workingdirectory/$basename.svg --png $workingdirectory/$basename.png --dot $workingdirectory/$basename.dot -o $workingdirectory/$basename.result --ivalue $ivalue $autocolor";
        $ivalue_string = "NOVALUE";
        if($autocolor == '--autocolor') { $ivalue_string = "i=$ivalue"; }
        $evalue_string = "E=$evalue";
        $future_html_command = "$visBlast2path" . "/makehtml.pl $basename $evalue_string $ivalue_string > work/$basename.html";
        #echo "<b>$future_html_command</b><br>\n";
        flush();
        sleep(1);
        echo "      <div id=\"failsafe_announce\">Be patient with large requests.  After completion, results are displayed below.  They will be availible after completion at the following address even if your connection is lost.<br><span class=\"no_click_url\">$working_address" . "/$basename.html</span></div>\n";
        flush(); #display so far
        sleep(2);
        echo "      <div id=\"output_title\">OUTPUT</div>\n";
        $futurehtml = `$future_html_command`;
        $lines = `$command`;
        #echo "Finished executing<br\n";
        echo "      <div id=\"graphic_output\">\n";
        echo "        <div id=\"in_title\">$title<br>E=$evalue";
        if($autocolor == '--autocolor') { echo " i=$ivalue"; }
        echo "</div>\n";
        echo "        <div id=\"graph_row\">\n";
        echo "          <div id=\"graph_image\" class=\"tcell\"><img src=\"work/$basename.png\" class=\"graph_image_img\"></div>\n";
        echo "          <div id=\"legend_image\" class=\"tcell\"><img src=\"work/$basename.png.legend.png\" class=\"legend_image_img\"></div>\n";
        echo "        </div>\n";
        echo "      </div>\n";
        echo "      <div id=\"downloads\">\n";
        echo "        <div id=\"download_tile\">Download files</div>\n";
        echo "        <div id=\"fasta_file\" class=\"file_link\"><a href=\"work/$basename.fasta\">Fasta file</a></div>\n";
        echo "        <div id=\"blast_file\" class=\"file_link\"><a href=\"work/$basename.result\">Blast search results</a></div>\n";
        echo "        <div id=\"dot_file\" class=\"file_link\"><a href=\"work/$basename.dot\">Dot format network file</a></div>\n";
        echo "        <div id=\"image_file\" class=\"file_link\"><a href=\"work/$basename.png\">Image file</a></div>\n";
        echo "        <div id=\"legend_file\" class=\"file_link\"><a href=\"work/$basename.png.legend.png\">Legend image file</a></div>\n";
        echo "        <div class=\"file_link\"><a href=\"work/$basename.svg\">Labeled SVG image file</a></div>\n";
        if($autocolor == '--autocolor') { 
          echo "        If MCL-HMM generator is on...\n";
          echo "        <div class=\"file_link\"><a href=\"work/$basename-fasta.zip\">MCL grouped fasta files</a></div>\n";
          echo "        <div class=\"file_link\"><a href=\"work/$basename-selex.zip\">MCL grouped alignment files</a></div>\n";
          echo "        <div class=\"file_link\"><a href=\"work/$basename-hmm.zip\">MCL groups as hidden markov models</a></div>\n";
        }
        echo "      </div>\n";
        if($fasta != NULL && $title != NULL && $evalue != NULL) {
          echo "      <div id=\"reference\">Jason Weirather, Mary Wilson, John Donelson.  Manuscript in preparation 2010.</div>\n";
        }
      } else {
        if($haveinputs == 0 && $no_error == 0) {
          echo "<div id=\"problem\">FIX THIS:  Enter Title, E-value, and Fasta please. ";
          if($fastaCheck != NULL && $fastaCheck != 'good') { echo "<br>Specifically, " . $fastaCheck; }
          echo "</div>";
        }
      } 
      echo ('
      <div id="input_title">INPUT</div>
      <div id="input_form">
        <form enctype="multipart/form-data" action="'.$self.'" method="post">
          <span class="form_label">Title: </span><input type="text" name="title" value="Homologous clustering" id="title_text_box"><br>
          <div id="paramater_row">
            <div id="evalue_input"><span class="form_label">E-value:</span><input type="text" name="evalue" value="1e-3" id="evalue_text_box"></div>
            <div id = "autocolor_choices">
              <div id="autocolor_checkbox"><span class="ivalue_form_label">MCL-HMM Autocolor </span><input type="checkbox" id="ibox" name="autocolor" value="doauto"></div>
              <div id="ivalue"><span class="ivalue_form_label">i-value (1.2-5):</span><input type="text" name="ivalue" value="2.0" id="ivalue_text_box"></div>
            </div>
          </div>
          <div id="fasta_input">
            <span class="form_label">Fasta (paste here or browse for a file)</span><br>
            <textarea name="fasta" id="fasin" value="5"></textarea><br><br>
            <input type="hidden" name="MAX_FILE_SIZE" value="500000000" />
<input name="fastainf" type="file" size="10"/>&nbsp&nbsp<input type="submit" value="Submit"/>&nbsp&nbsp<a href="visblasthelp.html" id="help">?</a>
          </div>
        </form>
      </div>
    ');
      if($fasta == NULL || $title == NULL || $evalue == NULL) {
        echo "<div id=\"reference\">$ref_string</div>\n";
        include('lastline.php');
      }
      echo "</div>\n";
      function checkEvalue($ineval) {
        if(is_numeric($ineval)) { $outeval =(float)$ineval; }
        else { $outeval = NULL; }
        return $outeval;
      }
      function checkFasta($fasta) {
      $max_sequence_length = 10000;
      $min_sequence_length = 2;
      $max_number_of_sequences = 5000000;
      $min_number_of_sequences = 2;
        $check = 'good';
        $lines = explode("\n",$fasta);
        foreach($lines as &$line) {
           $line = trim($line);
           if(preg_match('/^>\S+/',$line)) { } #proper title
           else { 
             if(!preg_match('/\S/',$line)) {  }#ok if its empty
             else {
               if(preg_match('/^[GAVLIMFWPSTCYNQDEKRHUX\*gavlimfwpstcynqdekrhux\s]+$/',$line)) {  #ok if its a gene or a nucleotide with whitespace on the ends
               } else { 
                 $check = "Error - Illegal Character: <br>" . wordwrap(htmlentities($line),50,"<br>",1);
               }
             }
           }
        }
        unset($line);
        $lines = preg_split('/>.*\n/',$fasta);
        if(sizeof($lines) > $max_number_of_sequences) { $check = "Error - Too many sequences.  " . sizeof($lines) . " exceeds $max_number_of_sequences maximum number of sequences."; }
        if(sizeof($lines) < $min_number_of_sequences) { $check = "Error - Too few sequences.  " . sizeof($lines) . " is less than $min_number_of_sequences minimum number of sequences."; }
        $countthem = 1;
        foreach($lines as &$line) {
          $one_seq = preg_replace('/\r|\n|\s/',"",$line);
          if(strlen($one_seq) > $max_sequence_length) { $check = "Error $countthem - Sequence needs to be shorter than $max_sequence_length characters: <br>" . wordwrap(htmlentities($one_seq),50,"<br>",1); }
          //if(strlen($one_seq) < $min_sequence_length) { $check = "Error $countthem - Sequence needs to be longer than $min_sequence_length characters: <br>" . wordwrap(htmlentities($one_seq),50,"<br>",1); }
          $countthem++;
        }
        unset($line);
        return $check;
      }
    ?>
  </body>
</html> 
